package com.dps.omc.publish;

import com.dps.omc.model.Agent;
import com.dps.omc.model.Server;
import com.dps.omc.model.Task;
import com.dps.omc.model.TelnetServer;
import com.dps.omc.mq.producer.IProducerService;
import com.dps.omc.task.message.*;
import com.dps.omc.utils.RegexpUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by lip on 2016-01-20.
 */
@SuppressWarnings("unchecked")
@Service("MsgManager")
public class MsgManager {
    @Autowired
    private IProducerService producerService;

    public void sendCommandMessageByDbServer(Task task, Agent agent, String shell) throws Exception {
        String ip = agent.getIp();
        String subject = EnumMsgType.COMMAND.getValue()+"_"+ip;
        String msg = this.getCommandMsgByDbServer(task,agent,shell);
        producerService.sendMessage(subject,msg,task.getSeq());
    }

    public void sendCommandMessageByServer(Task task, Agent agent, Server server, String shell) throws Exception {
        String ip = agent.getIp();
        String subject = EnumMsgType.COMMAND.getValue()+"_"+ip;
        String msg = this.getCommandMsgByServer(task,agent,server,shell);
        producerService.sendMessage(subject,msg,task.getSeq());
    }

    public void sendCommandMessageByTelnetServer(Task task, Agent agent, TelnetServer telnetServer, String shell) throws Exception {
        String ip = agent.getIp();
        String subject = EnumMsgType.COMMAND.getValue()+"_"+ip;
        String msg = this.getCommandMsgByTelnetServer(task,agent,telnetServer,shell);
        producerService.sendMessage(subject,msg,task.getSeq());
    }

    private String getCommandMsgByDbServer(Task task, Agent agent, String shell) throws Exception {
        TaskMessage taskMessage = new TaskMessage();

        taskMessage.setTaskId(task.getId());
        taskMessage.setCmdShell(shell);
        taskMessage.setAgentId(agent.getId());
        taskMessage.setClientIp(agent.getIp());
        taskMessage.setSysUser(agent.getUserName());
        taskMessage.setSysPass(agent.getPassword());
        taskMessage.setMsgType(EnumMsgType.COMMAND);
        taskMessage.setReturnMsg("");
        taskMessage.setTaskState(EnumTaskState.NEW);
        taskMessage.setTaskNodeType(EnumTaskNodeType.DBSERVER);
        this.processTaskMessageByPass(taskMessage);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("taskMessage",taskMessage);
        return jsonObject.toString();
    }


    private String getCommandMsgByServer(Task task, Agent agent, Server server, String shell) throws Exception {
        TaskMessage taskMessage = new TaskMessage();

        taskMessage.setTaskId(task.getId());
        taskMessage.setCmdShell(shell);
        taskMessage.setAgentId(agent.getId());
        taskMessage.setClientIp(agent.getIp());
        taskMessage.setSysUser(agent.getUserName());
        taskMessage.setSysPass(agent.getPassword());
        taskMessage.setMsgType(EnumMsgType.COMMAND);
        taskMessage.setReturnMsg("");
        taskMessage.setTaskState(EnumTaskState.NEW);
        taskMessage.setServerConn(true);
        taskMessage.setServerIp(server.getHost());
        taskMessage.setServerUser(server.getUser());
        taskMessage.setServerPass(server.getPass());
        taskMessage.setTaskNodeType(EnumTaskNodeType.SERVER);
        this.processTaskMessageByPass(taskMessage);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("taskMessage",taskMessage);
        return jsonObject.toString();
    }

    private String getCommandMsgByTelnetServer(Task task, Agent agent, TelnetServer telnetServer, String shell) throws Exception {
        TaskMessage taskMessage = new TaskMessage();

        taskMessage.setTaskId(task.getId());
        taskMessage.setCmdShell(shell);
        taskMessage.setAgentId(agent.getId());
        taskMessage.setClientIp(agent.getIp());
        taskMessage.setSysUser(agent.getUserName());
        taskMessage.setSysPass(agent.getPassword());
        taskMessage.setMsgType(EnumMsgType.COMMAND);
        taskMessage.setReturnMsg("");
        taskMessage.setTaskState(EnumTaskState.NEW);
        taskMessage.setServerConn(true);
        taskMessage.setServerIp(telnetServer.getHost());
        taskMessage.setServerUser(telnetServer.getUser());
        taskMessage.setServerPass(telnetServer.getPass());
        taskMessage.setTaskNodeType(EnumTaskNodeType.TELNET);
        this.processTaskMessageByPass(taskMessage);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("taskMessage",taskMessage);
        return jsonObject.toString();
    }

    private void processTaskMessageByPass(TaskMessage tm) throws Exception {
        if(StringUtils.isNotBlank(tm.getSysPass()))
        {
            tm.setSysPass(DesUtils.decrypt(tm.getSysPass()));
        }
        if(StringUtils.isNotBlank(tm.getServerPass()))
        {
            tm.setServerPass(DesUtils.decrypt(tm.getServerPass()));
        }
        if(StringUtils.isNotBlank(tm.getCmdShell()))
        {
            tm.setCmdShell(RegexpUtil.filter(tm.getCmdShell()));
        }
    }

}
